<?php
/*
	Copyright 2006, 2007, 2008, 2009, 2010 Bastiaan Grutters
    
    This file is part of Ages of Strife website.

    Ages of Strife website is free software: you can redistribute it and/or modify
    it under the terms of the GNU General Public License as published by
    the Free Software Foundation, either version 3 of the License, or
    (at your option) any later version.

    Ages of Strife website is distributed in the hope that it will be useful,
    but WITHOUT ANY WARRANTY; without even the implied warranty of
    MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
    GNU General Public License for more details.

    You should have received a copy of the GNU General Public License
    along with Ages of Strife website.  If not, see <http://www.gnu.org/licenses/>.
 */
include( "../global/initialize.php");

$officer_name = formatInput( $_GET[ 'name' ] );
if( !isset( $officer_name ) || $officer_name == '' ) {
	header( "Location: armies.php" );
	return;
}

aos_header( 'Officer' );

include ( "../global/resources.php" );
print( "<br />\r\n" );
$query = "SELECT * " .
		"FROM officers " .
		"WHERE name = '" . $officer_name . "'";
$result = mysql_query( $query ) or die( "Query failed : " . mysql_error() );
$row = mysql_fetch_array($result, MYSQL_ASSOC);
if( $row[ 'ruler_id' ] == $_SESSION[ 'ruler_id' ] ) {
	$officer_id = $row[ "officer_id" ];
	$rank = $row[ 'rank' ];
	$experience = $row[ 'experience' ];

	$query2 = "SELECT * " .
			"FROM officers " .
			"WHERE ruler_id = " . $_SESSION['ruler_id'] . " ORDER BY rank DESC, name";
	$result2 = mysql_query( $query2 ) or die( "Query failed : " . mysql_error() );
	$num2 = mysql_numrows( $result2 );
	$i = 0;
	while ( $i < $num2 ) {
		if( mysql_result( $result2, $i, "officer_id" ) == $officer_id ) {
			if( $i < $num2 - 1 ) {
				print( "<div class=\"text-right\"><a href=\"officer.php?name=" . mysql_result( $result2, ( $i + 1 ), "name" ) . "\">" . translate( 'Next' ) . "</a></div>\r\n" );
			}
			if( $i > 0 ) {
				print( "<div class=\"text-left\"><a href=\"officer.php?name=" . mysql_result( $result2, ( $i - 1 ), "name" ) . "\">" . translate( 'Previous' ) . "</a></div>\r\n" );
			}
?>
						<div class="text-center">
							<div class="officer-name-title">
								<?php printOfficerRankIcon( $rank ); ?>
								<h2><?php print( $officer_name ); ?></h2>
							</div>
						</div>
<?php			
		}
		$i ++;
	}
	print "<div class=\"blok\">\r\n";
	print "<form action=\"change_rank.php\" method=\"post\">\r\n";
	print translate( "Rank" ) . ": <select name=\"rank\" id=\"rank_select\">\r\n";
	
	$max_rank = 3 + $row[ 'ranking' ];
	
	$selected = "";
	if( $rank == 0 ) {
		$selected = "selected";
	}
	print "<option $selected value=\"2nd Lieutenant\">" . translate( "2nd Lieutenant" ) . "</option>\r\n";
	
	$selected = "";
	if( $rank == 1 ) {
		$selected = "selected";
	}
	print "<option $selected value=\"1st Lieutenant\">" . translate( "1st Lieutenant" ) . "</option>\r\n";
	
	if( $rank == 2 ) {
		$selected = "selected";
	}
	else {
		$selected = "";
	}
	print "<option $selected value=\"Captain\">" . translate( "Captain" ) . "</option>\r\n";
	if( $max_rank > 3 ) {
		if( $rank == 3 ) {
			$selected = "selected";
		}
		else {
			$selected = "";
		}
		print "<option $selected value=\"Major\">" . translate( "Major" ) . "</option>\r\n";
		if( $max_rank > 4 ) {		
			if( $rank == 4 ) {
				$selected = "selected";
			}
			else {
				$selected = "";
			}
			print "<option $selected value=\"Lieutenant-Colonel\">" . translate( "Lieutenant-Colonel" ) . "</option>\r\n";
			if( $max_rank > 5 ) {	
				if( $rank == 5 ) {
					$selected = "selected";
				}
				else {
					$selected = "";
				}
				print "<option $selected value=\"Colonel\">" . translate( "Colonel" ) . "</option>\r\n";
				if( $max_rank > 6 ) {
					if( $rank == 6 ) {
						$selected = "selected";
					}
					else {
						$selected = "";
					}
					print "<option $selected value=\"General\">" . translate( "General" ) . "</option>\r\n";
					if( $max_rank > 7 ) {
						if( $rank == 7 ) {
							$selected = "selected";
						}
						else {
							$selected = "";
						}
						print "<option $selected value=\"Field Marshal\">" . translate( "Field Marshal" ) . "</option>\r\n";
					}
				}
			}
		}
	}
	print "</select> \r\n";
	print "<input type=\"hidden\" name=\"officer_id\" value=\"$officer_id\" />\r\n";
	print "<input type=\"submit\" value=\"" . translate( "Change rank" ) . "\" />\r\n";
	print "</form><br/><br/>\r\n";

	print( translate( "Daily wage" ) . ": <strong class=\"gold\">" . number_format( 2 + $rank * 3 ) . "</strong><br />" );
	
	$max_size = 10 * pow( 5, ( $rank + 1 ) );
	$army_size = "";
	$query3 = "SELECT scout, infantry, cavalry, artillery, x, y, travel " .
			"FROM army " .
			"WHERE officer_id = $officer_id";
	$result3 = mysql_query( $query3 ) or die( "Query failed : " . mysql_error() );
	$row3 = mysql_fetch_array($result3, MYSQL_ASSOC);
	$strong = '';
	$end = '';
	if( isset( $row3[ 'scout' ] ) ) {
		if( $row3[ 'scout' ] + $row3[ 'infantry' ] + $row3[ 'cavalry' ] + $row3[ 'artillery' ] > $max_size ) {
			$strong = '<strong class="red">';
			$end = '</strong>';
		}
		$army_size = $strong . translate( "Current army size" ) . ": " . number_format( $row3[ 'scout' ] + $row3[ 'infantry' ] + $row3[ 'cavalry' ] + $row3[ 'artillery' ] ) . "$end<br />";
	}
	print( $strong . translate( "Maximum army size" ) . ': ' .  number_format( $max_size ) . $end . "<br />$army_size<br />"  );	
	
	print( translate( "Experience" ) . ": " . number_format( $experience ) . "<br/><br />\r\n" );
	$query2 = "SELECT faction_id " .
			"FROM faction_member " .
			"WHERE ruler_id = " .$_SESSION[ 'ruler_id' ] . " AND invited = 0 AND requested = 0";
	$result2 = mysql_query( $query2 ) or die( "Query failed : " . mysql_error() );
	$row2 = mysql_fetch_array($result2, MYSQL_ASSOC);
	if( isset( $row2[ 'faction_id' ] ) ) {
		$faction_id = $row2[ 'faction_id' ];
	}
	else {
		$faction_id = -1;
	}
	
	$academy_available = false;
	if( isset( $row3[ 'travel' ] ) && $row3[ 'travel' ] == 0 && isset( $row3[ 'x' ] ) ) {
		$query2 = "SELECT name, village.ruler_id " .
				"FROM village " .
				"JOIN village_buildings ON village.village_id = village_buildings.village_id ";
		( $faction_id != -1 ) ? $query2 .= "JOIN faction_member ON faction_member.ruler_id = village.ruler_id " : "";
		$query2 .= "WHERE x = " . $row3[ 'x' ] . " AND y = " . $row3[ 'y' ] . " AND building_id = 24 ";
		( $faction_id != -1 ) ? $query2 .= "AND faction_id = $faction_id AND requested = 0 AND invited = 0 " : " AND village.ruler_id = " . $_SESSION[ 'ruler_id' ] . " ";
		$result2 = mysql_query( $query2 ) or die( "Query failed : " . mysql_error() );
		$row2 = mysql_fetch_array( $result2, MYSQL_ASSOC );
		if( isset( $row2[ 'name' ] ) ) {
			$academy_available = true;
			if( $row2[ 'ruler_id' ] == $_SESSION[ 'ruler_id' ] ) {
				$academy = '<a href="../villages/village.php?name=' . $row2[ 'name' ] . '"><strong class="green">' . $row2[ 'name' ] . '</strong></a>';
			}
			else {
				$academy = '<strong class="blue">' . $row2[ 'name' ] . '</strong>';
			}
		}
	}
	else {
		$query2 = "SELECT name " .
				"FROM village " .
				"JOIN village_buildings ON village.village_id = village_buildings.village_id " .
				"WHERE building_id = 24 AND start_place = 1 AND ruler_id = " . $_SESSION[ 'ruler_id' ];
		$result2 = mysql_query( $query2 ) or die( "Query failed : " . mysql_error() );
		$row2 = mysql_fetch_array( $result2, MYSQL_ASSOC );
		if( isset( $row2[ 'name' ] ) ) {
			$academy_available = true;
			$academy = '<a href="../villages/village.php?name=' . $row2[ 'name' ] . '"><strong class="green">' . $row2[ 'name' ] . '</strong></a>';
		}
	}
	
	if( $academy_available ) {
		$query4 = "SELECT gold " .
				"FROM ruler " .
				"WHERE ruler_id = " . $_SESSION[ 'ruler_id' ];
		$result4 = mysql_query( $query4 ) or die( "Query failed : " . mysql_error() );
		$row4 = mysql_fetch_array( $result4, MYSQL_ASSOC );
		$gold = $row4[ 'gold' ];
?>
					<strong><?php transprint( 'Courses available at the military academy of' ); ?> <?php print( $academy ); ?></strong><br />
					<form method="post" action="attend_training.php">
						<input type="hidden" name="officer_id" value="<?php print( $officer_id ); ?>" />
<?php
		if( $gold >= 2000 ) {						
?>
						<input type="submit" name="small_training" value="<?php transprint( 'Attend regular tactics training' ); ?>" /> - <?php transprint( 'Adds 1,000 experience at a price of <strong class="gold">2,000</strong> gold.' ); ?><br />
<?php
		}
		else {
?>
						<?php transprint( 'We don\'t have enough gold to allow this officer to attend any training.' ); ?><br />
<?php			
		}
		
		if( $gold >= 9000 ) {						
?>
						<input type="submit" name="large_training" value="<?php transprint( 'Attend advanced tactics training' ); ?>" /> - <?php transprint( 'Adds 5,000 experience at a price of <strong class="gold">9,000</strong> gold.' ); ?><br />
<?php
		}
?>
					</form>
					<br />
<?php
	}
	else {			
		$query2 = "SELECT DISTINCT village.village_id, village.name, village.x, village.y " .
				"FROM village " .
				"JOIN village_buildings ON village_buildings.village_id = village.village_id ";
		( $faction_id != -1 ) ? $query2 .= "JOIN faction_member ON faction_member.ruler_id = village.ruler_id " : "";
		$query2 .= "WHERE village_buildings.building_id = 24 ";
		( $faction_id != -1 ) ? $query2 .= "AND faction_id = $faction_id AND requested = 0 AND invited = 0 " : " AND village.ruler_id = " . $_SESSION[ 'ruler_id' ] . " ";
		$query2 .= "ORDER BY village.name";
		$result2 = mysql_query( $query2 ) or die( "Query failed : " . mysql_error() );
		$num2 = mysql_numrows( $result2 );
		$i = 0;
		if( $num2 > 0 ) {
?>
					<strong><?php transprint( 'Available military academies' ); ?>:</strong><br />
					<div class="military-academy-list">
						<ul>
<?php
			while ( $i < $num2 ) {
				$location = new stdClass();
				$location->x = mysql_result( $result2, $i, 'x' );
				$location->y = mysql_result( $result2, $i, 'y' );
				$location->title = '';
?>
						<li><?php print( get_location_link( $location ) ); ?></li>
<?php
				$i ++;
			}
?>
						</ul>
					</div>
					<br />
<?php		
		}
		else {
?>
					<?php transprint( 'There are no military academies available for training.' ); ?><br />
					<br />
<?php		
		}
	}
	
	$free_skill_points = get_officer_free_skillpoints( $row[ 'officer_id' ], $row[ 'experience' ], $row[ 'scouting' ], $row[ 'offense' ], $row[ 'defense' ], $row[ 'ranking' ], $row[ 'persuasion' ] );
	
	if( $experience <= 90 ) {
		$skill_count = 0.5;
	}
	else {
		$skill_count = log( $experience / 40, 2.25 );
	}
	if( $skill_count <= 25 ) {
		if( $experience <= 90 ) {
			$start_experience = 0;
		}
		else {
			$start_experience = 40 * pow( 2.25, floor( $skill_count ) );
		}
		$next_level = 40 * pow( 2.25, ceil( $skill_count ) );
		$percentage = ( ( $row[ 'experience' ] - $start_experience ) / ( $next_level - $start_experience ) ) * 100.0;
		$pixels = round( $percentage * 5 );
		$percentage = round( $percentage, 2 );
		
?>
					<?php transprint( 'Next skill point' ); ?>:<br />
					<div class="experience-container">
						<div class="experience-box">
							<div class="experience-bar" style="width: <?php print( $pixels ); ?>px">
								<?php print( $percentage ); ?>%
							</div>
						</div>
						<div class="text-left"><?php print( number_format( $start_experience ) ); ?></div>
						<div class="text-right"><?php print( number_format( $next_level ) ); ?></div>
					</div>
					<br />
					<br />
<?php		
	}
	
	$extra_br = false;
	if( $free_skill_points > 0 ) {
		print( '<strong>' . translate( 'Skill points available' ) . ': ' . $free_skill_points . '</strong><br />' );
	}
	
	if( $row[ 'ranking' ] > 0 || $free_skill_points > 0 ) {
		$extra_br = true;
		print( translate( 'Ranking' ) . ' ' . translate( 'Level' ) . ' ' . $row[ 'ranking' ] );
		if( $free_skill_points > 0 && $row[ 'ranking' ] < 5 ) {
?>
					<form action="add_skill.php" method="post">
						<input type="hidden" name="officer_id" value="<?php print( $officer_id ); ?>" />
						<input type="hidden" name="skill" value="ranking" />
						<input type="submit" value="<?php transprint( 'Increase' ); ?>" />
					</form>
<?php 
		}
		print( ' <small>(' . translate( 'Allows this officer to get a higher rank each level' ) . ')</small><br />' );
	}
	if( $row[ 'offense' ] > 0 || $free_skill_points > 0 ) {
		$extra_br = true;
		print( translate( 'Offense' ) . ' ' . translate( 'Level' ) . ' ' . $row[ 'offense' ] );
		if( $free_skill_points > 0 && $row[ 'offense' ] < 5 ) {
?>
					<form action="add_skill.php" method="post">
						<input type="hidden" name="officer_id" value="<?php print( $officer_id ); ?>" />
						<input type="hidden" name="skill" value="offense" />
						<input type="submit" value="<?php transprint( 'Increase' ); ?>" />
					</form>
<?php 
		}
		print( ' <small>(' . translate( 'Gives this officers army 2% extra offense bonus in battles per level' ) . ')</small><br />' );
	}
	if( $row[ 'defense' ] > 0 || $free_skill_points > 0 ) {
		$extra_br = true;
		print( translate( 'Defense' ) . ' ' . translate( 'Level' ) . ' ' . $row[ 'defense' ] );
		if( $free_skill_points > 0 && $row[ 'defense' ] < 5 ) {
?>
					<form action="add_skill.php" method="post">
						<input type="hidden" name="officer_id" value="<?php print( $officer_id ); ?>" />
						<input type="hidden" name="skill" value="defense" />
						<input type="submit" value="<?php transprint( 'Increase' ); ?>" />
					</form>
<?php 
		}
		print( ' <small>(' . translate( 'Gives this officers army 2% extra defense bonus in battles per level' ) . ')</small><br />' );
	}
	if( $row[ 'scouting' ] > 0 || $free_skill_points > 0 ) {
		$extra_br = true;
		print( translate( 'Scouting' ) . ' ' . translate( 'Level' ) . ' ' . $row[ 'scouting' ] );
		if( $free_skill_points > 0 && $row[ 'scouting' ] < 5 ) {
?>
					<form action="add_skill.php" method="post">
						<input type="hidden" name="officer_id" value="<?php print( $officer_id ); ?>" />
						<input type="hidden" name="skill" value="scouting" />
						<input type="submit" value="<?php transprint( 'Increase' ); ?>" />
					</form>
<?php 
		}
		print( ' <small>(' . translate( 'Allows this officer to see armies 1 square further per level, at level 5 this skill allows the officer to gather information on any visible army every 12 turns.' ) . ')</small><br />' );
	}
	if( $row[ 'persuasion' ] > 0 || $free_skill_points > 0 ) {
		$extra_br = true;
		print( translate( 'Persuasion' ) . ' ' . translate( 'Level' ) . ' ' . $row[ 'persuasion' ] );
		if( $free_skill_points > 0 && $row[ 'persuasion' ] < 5 ) {
?>
					<form action="add_skill.php" method="post">
						<input type="hidden" name="officer_id" value="<?php print( $officer_id ); ?>" />
						<input type="hidden" name="skill" value="persuasion" />
						<input type="submit" value="<?php transprint( 'Increase' ); ?>" />
					</form>
<?php 
		}
		print( ' <small>(' . translate( 'Decreases bribing and conquest cost for villages by 10% per level' ) . ')</small><br />' );
	}	
	if( $extra_br ) {
		print( "<br />" );
	}
	
	// This officer can gather info from other armies
	if( $row[ 'scouting' ] == 5 ) {
		$turn = getCurrentTurn();
		$until = $turn - $row[ 'last_scouting' ];
		if( $until >= 12 ) {
?>
				<strong><?php transprint( 'Gather army information' ); ?></strong><br />
				<div id="officer-gather-information">
					<?php print_officer_gather_info_page( $officer_id ); ?>
				</div>
				<br />
<?php
		}
		else {
			$until = 12 - $until;
			if( $until == 1 ) {
				$until = translate( '1 day' );
			}
			else {
				$until = $until . ' ' . translate( 'days' );
			}
?>
				<?php transprint( 'Gathering information available again in' ); ?> <?php print( $until ); ?>.<br />
				<br />
<?php
		}
	}
	
	$title = "";
	if( $experience > 50000000 ) {
		$title = translate( "Reputation: Legendary" ) . "<br /><br />";
	}
	elseif( $experience > 5000000 ) {
		$title = translate( "Reputation: Famous" ) . "<br /><br />";
	}
	elseif( $experience > 500000 ) {
		$title = translate( "Reputation: Well known" ) . "<br /><br />";
	}
	print( $title );
	
//	$attackBonus = 0;
//	$defenseBonus = 0;
//	if( $experience > 0 ) {
//		for( $i = 1; $i < 15; $i ++ ) {
//			if( $experience > pow( 10, $i ) ) {
//				$attackBonus += 1;
//			}
//			if( $experience > 5 * pow( 10, $i ) ) {
//				$defenseBonus += 1;
//			}
//		}
//		if( $attackBonus > 0 || $defenseBonus > 0 ) {
//			print( translate( "Officers increased combat performance from experience" ) . ":<br />\r\n" );
//			print( translate( "Attack bonus" ) . ": " . $attackBonus . "%<br />\r\n" );
//			print( translate( "Defense bonus" ) . ": " . $defenseBonus . "%<br />\r\n" );
//		}
//	}
	
	$risk = $row[ 'risks' ];
	if( $row[ 'global_risks' ] == 1 ) {
		$risk = -1;
	}
	$query = "SELECT global_risk " .
			"FROM ruler " .
			"WHERE ruler_id = " . $_SESSION[ 'ruler_id' ];
	$result = mysql_query( $query ) or die( "Query failed : " . mysql_error() );
	$row = mysql_fetch_array($result, MYSQL_ASSOC);	
	$global_risk = $row[ 'global_risk' ];
	if( $global_risk == 0 ) {
		$global_risk_text = translate( 'No' );
	}
	else {
		$global_risk_text = $global_risk * 10 . '%';
	}
?>
			<?php transprint( 'This officer will take' ); ?> <form method="post" action="set_officer_risk.php">
				<input type="hidden" name="officer_id" value="<?php print( $officer_id ); ?>" />
				<select name="risk" onchange="this.parentNode.submit();">
					<option value="-1"<?php ( $risk == -1 ) ? print( ' selected="selected"' ) : print( '' ); ?>><?php print( $global_risk_text ); ?> (<?php transprint( 'global setting' ); ?>)</option>
					<option value="0"<?php ( $risk == 0 ) ? print( ' selected="selected"' ) : print( '' ); ?>><?php transprint( 'no' ); ?></option>
					<option value="1"<?php ( $risk == 1 ) ? print( ' selected="selected"' ) : print( '' ); ?>>10%</option>
					<option value="2"<?php ( $risk == 2 ) ? print( ' selected="selected"' ) : print( '' ); ?>>20%</option>
					<option value="3"<?php ( $risk == 3 ) ? print( ' selected="selected"' ) : print( '' ); ?>>30%</option>
					<option value="4"<?php ( $risk == 4 ) ? print( ' selected="selected"' ) : print( '' ); ?>>40%</option>
					<option value="5"<?php ( $risk == 5 ) ? print( ' selected="selected"' ) : print( '' ); ?>>50%</option>
				</select> <?php transprint( 'risk' ); ?>.<br />
			</form>
			<br />
			<?php transprint( 'In battle this officer will get' ); ?>:<br />
<?php
	if( $risk == -1 ) {
		$risk = $global_risk;
	}
	$chance = 0;
	$level = 0;
	if( $experience >      50000000 ) {
		$chance = 0.05;
		$level = 5;
	}
	else if( $experience > 25000000 ) {
		$chance = 0.04;
		$level = 4;
	}
	else if( $experience > 12500000 ) {
		$chance = 0.03;
		$level = 3;
	}
	else if( $experience >  6250000 ) {
		$chance = 0.02;
		$level = 2;
	}
	else if( $experience >  3125000 ) {
		$chance = 0.01;
		$level = 1;
	}
	
	if( $risk == 0 ) {
?>
			<?php print( $chance ); ?>% <?php transprint( 'offense and defense bonus' ); ?><br />
<?php		
	}
	else {
?>
			<?php transprint( 'either' ); ?><br />
			<?php print( ( $risk * 10 ) . '%' ); ?> <?php transprint( 'offense and defense bonus' ); ?><br />
			<?php transprint( 'or' ); ?><br />
			<?php print( ( $risk * -10 ) . '%' ); ?> <?php transprint( 'offense and defense bonus' ); ?><br />
<?php		
	}
?>
			<br />
			<?php transprint( 'Officers risk management skills' ); ?>: <?php transprint( 'level' ); ?> <?php print( $level ); ?><br />
			<?php transprint( 'Higer levels of risk management will increase the chance to get a positive bonus or will increase the bonus when no risk setting is used.' ); ?><br />
			<?php transprint( 'Risk management skill will increase when an officer gains more experience.' ); ?> <small>(<a href="/aosmanual/concepts.php#risk"><?php transprint( 'Read about risk in the manual' ); ?></a>)</small><br />
			<br />
<?php
	$query = "SELECT * FROM transfer WHERE officer_id = " . $officer_id;
	$result = mysql_query( $query ) or die( "Query failed : " . mysql_error() );
	$row = mysql_fetch_array($result, MYSQL_ASSOC);	
	
	if( isset( $row[ 'transfer_id' ] ) ) {
		$game_turn = getCurrentTurn();
		$complete_turn = $row[ 'complete_turn' ];
		$time_remaining = $complete_turn - $game_turn;
		if( $row[ 'from_army_id' ] == -1 ) { // joining an army
			$query = "SELECT * FROM army WHERE army_id = " . $row[ 'to_army_id' ];
			$result = mysql_query( $query ) or die( "Query failed : " . mysql_error() );
			$row = mysql_fetch_array($result, MYSQL_ASSOC);	
			print( translate( "Taking command of" ) . " <a href=\"../armies/army.php?name=" . $row[ 'name' ] . "\">" . $row[ 'name' ] . "</a> ($time_remaining " . translate( "days remaining" ) . ")<br />\r\n" );
		}
		elseif( $row[ 'to_army_id' ] == -1 ) {
			$query = "SELECT * FROM army WHERE army_id = " . $row[ 'from_army_id' ];
			$result = mysql_query( $query ) or die( "Query failed : " . mysql_error() );
			$row = mysql_fetch_array($result, MYSQL_ASSOC);	
			print( translate( "Leaving command of" ) . " <a href=\"../armies/army.php?name=" . $row[ 'name' ] . "\">" . $row[ 'name' ] . "</a> ($time_remaining " . translate( "days remaining" ) . ")<br />\r\n" );
		}
	}
	else {
		$query = "SELECT * " .
				"FROM army " .
				"WHERE officer_id = " . $officer_id;
		$result = mysql_query( $query ) or die( "Query failed : " . mysql_error() );
		$row = mysql_fetch_array($result, MYSQL_ASSOC);	
		
		if( isset( $row[ 'army_id' ] ) ) {
			$army = "<a href=\"army.php?name=" . $row[ 'name' ] . "\">" . $row[ 'name' ] . "</a>";
			$army_id = $row[ 'army_id' ];
			print( translate( "Commanding" ) . ": $army<br /><br />\r\n" );
	
			print "<form action=\"change_army.php\" method=\"post\">\r\n";
			print "<input type=\"hidden\" name=\"from_army\" value=\"$army_id\" />\r\n";
			print "<input type=\"hidden\" name=\"officer_id\" value=\"$officer_id\" />\r\n";
			print "<input type=\"checkbox\" name=\"remove_officer\" value=\"true\" /> " . translate( 'Remove this officer from the army' ) . "<br />\r\n";
			print "<input type=\"submit\" value=\"" . translate( "Remove from army" ) . "\" />\r\n";
			print "</form><br/>\r\n";
			print( "<small>(" . translate( "By removing an officer from an army with soldiers in it the soldiers will desert and eventually come back to your reserves" ) . ")</small><br />" );
		}
		else {
			$query = "SELECT count(*) " .
					"FROM transfer " .
					"WHERE ruler_id = " . $_SESSION[ 'ruler_id' ] . " AND officer_id != -1";
			$result = mysql_query( $query ) or die( "Query failed : " . mysql_error() );
			$row = mysql_fetch_array($result, MYSQL_ASSOC);
			$remove = $row[ 'count(*)' ];
			
			$query = "SELECT army_id, army.name " .
					"FROM army " .
					"JOIN village ON army.x = village.x AND army.y = village.y AND village.ruler_id = army.ruler_id " .
					"WHERE army.ruler_id = " . $_SESSION['ruler_id'] . " AND officer_id = -1 AND travel = 0 " .
					"ORDER BY army.name";
			$result = mysql_query( $query ) or die( "Query failed : " . mysql_error() );
			$num = mysql_numrows( $result );
			
			$i = 0;
			if( $num - $remove > 0 ) {
				print "<form action=\"change_army.php\" method=\"post\">\r\n";
				print translate( "Put in command of" ) . ": <select name=\"to_army\">\r\n";
				//print "<option value=\"None\">None</option>\r\n";
			
				while ( $i < $num ) {
					$query2 = "SELECT count(*) FROM transfer WHERE to_army_id = " . mysql_result( $result, $i, "army_id" ) . " AND officer_id != -1";
					$result2 = mysql_query( $query2 ) or die( "Query failed : " . mysql_error() );
					$row2 = mysql_fetch_array($result2, MYSQL_ASSOC);
					if( $row2[ 'count(*)' ] == 0 ) {
						print "<option value=\"" . mysql_result( $result, $i, "army_id" ) . "\">" . mysql_result( $result, $i, "name" ) . "</option>";
					}
					$i ++;
				}
				print "</select> \r\n";
				print "<input type=\"hidden\" name=\"officer_id\" value=\"$officer_id\" />\r\n";
				print "<input type=\"submit\" value=\"" . translate( "confirm" ) . "\" />\r\n";
				print "</form><br/><br/>\r\n";
			}

			$query = "SELECT army_id, army.name " .
					"FROM army " .
					"JOIN village ON army.x = village.x AND army.y = village.y AND village.ruler_id = army.ruler_id " .
					"WHERE army.ruler_id = " . $_SESSION['ruler_id'] . " AND travel = 0 " .
					"ORDER BY army.name";
			$result = mysql_query( $query ) or die( "Query failed : " . mysql_error() );
			$num = mysql_numrows( $result );
			
			$i = 0;
			if( $num - $remove > 0 ) {
				print "<form action=\"change_army.php\" method=\"post\">\r\n";
				print translate( "Take over command of" ) . ": <select name=\"to_army\">\r\n";
				//print "<option value=\"None\">None</option>\r\n";
			
				while ( $i < $num ) {
					$query2 = "SELECT count(*) FROM transfer WHERE to_army_id = " . mysql_result( $result, $i, "army_id" ) . " AND officer_id != -1";
					$result2 = mysql_query( $query2 ) or die( "Query failed : " . mysql_error() );
					$row2 = mysql_fetch_array($result2, MYSQL_ASSOC);
					if( $row2[ 'count(*)' ] == 0 ) {
						print "<option value=\"" . mysql_result( $result, $i, "army_id" ) . "\">" . mysql_result( $result, $i, "name" ) . "</option>";
					}
					$i ++;
				}
				print "</select> \r\n";
				print "<input type=\"hidden\" name=\"officer_id\" value=\"$officer_id\" />\r\n";
				print "<input type=\"submit\" value=\"" . translate( "confirm" ) . "\" />\r\n";
				print "</form><br/><br/>\r\n";
			}
			
			print( translate( "Remove this officer from your service" ) . ": <a href=\"remove_officer.php?id=$officer_id\"><img src=\"../resources/delete.png\" border=\"0\"></a><br/>\r\n" );
		}
	}	
}
else {
?>
					<div class="blok">
						<?php transprint( 'Invalid officer name supplied.' ); ?><br />
						<?php transprint( 'If you followed an ingame link here, please report the problem.' ); ?>
<?php	
}
?>
					</div>
				<script type="text/javascript" src="officer.js"></script>
<?php
aos_footer();
?>